package com.edu.vod.service.impl;




import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.edu.eduservicebase.exception.GuliException;
import com.edu.vod.service.VideoService;
import com.edu.vod.utils.ConstantVodUtils;

import com.edu.vod.utils.createUploadVideo;
import com.edu.vod.utils.initVodClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
public class VideoServiceImpl implements VideoService {


    /**
     * 视频上传
     *
     * @param file
     * @return
     */
    @Override
    public String uploadVideoAly( MultipartFile file) {
        String filename = file.getOriginalFilename();
        String title = filename.substring(0, filename.lastIndexOf("."));
        String fileName = file.getOriginalFilename();
        //初始化
        DefaultAcsClient client = initVodClient.initVodClient(ConstantVodUtils.KEYID, ConstantVodUtils.KEYSEC);
        CreateUploadVideoResponse response = new CreateUploadVideoResponse();
        String vidoe=null;
        try {
            response = createUploadVideo.createUploadVideo(client, title, fileName);
            vidoe=response.getVideoId();
            System.out.print("VideoId = " + response.getVideoId() + "\n");
            System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
            System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
        System.out.print("RequestId = " + response.getRequestId() + "\n");
        return vidoe;
    }


    /**
     * 删除视频
     * @param videoId
     */
    @Override
    public void removeVideo(String videoId) {
        try {
            DefaultAcsClient client = initVodClient.initVodClient(ConstantVodUtils.KEYID, ConstantVodUtils.KEYSEC);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);

            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");

        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "视频删除失败");

        }

    }

    /**
     * 批量删除视频!
     * @param videoIdList
     */
    @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = initVodClient.initVodClient(ConstantVodUtils.KEYID, ConstantVodUtils.KEYSEC);
            //创建请求对象 一次只能批量删除20个
            DeleteVideoRequest request = new DeleteVideoRequest();
            String str = StringUtils.join(videoIdList.toArray(), ",");

            request.setVideoIds(str);
            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.println("删除视频： RequestId =" + response.getRequestId() + "\n" );

        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "批量视频删除失败!");

        }


    }
}
